package com.gzdh.dishrecognition.ui.pre;

import android.content.Context;
import android.os.Build;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.model.LcReportDto;
import com.gzdh.dishrecognition.ui.adapter.report.LcReportAdapter;
import com.gzdh.dishrecognition.ui.base.BasePresentation;

import java.util.List;

public class LcReportPre extends BasePresentation {

    ImageView iv_qrcode;

    TextView deviceSn;

    private RelativeLayout msgRv;
    private LinearLayout linearLayout;
    private TextView dateTopTv;
    private LinearLayout dateMsgLinear;
    private RecyclerView recyclerView;
    private LcReportAdapter lcReportAdapter;


    public LcReportPre(Context outerContext, Display display) {
        super(outerContext, display);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.pre_report_lc;
    }

    @Override
    protected void initView() {
        msgRv = findViewById(R.id.msgRv);
        linearLayout = findViewById(R.id.dateLinear);
        dateTopTv = findViewById(R.id.dateTopTv);
        dateMsgLinear = findViewById(R.id.dateMsgLinear);
        recyclerView = findViewById(R.id.recyclerView);

    }

    @Override
    protected void initData() {
        lcReportAdapter = new LcReportAdapter(this.getContext(), 5);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
        recyclerView.setLayoutManager(layoutManager);
    }

    public void showError(String error) {
        msgRv.setVisibility(View.VISIBLE);
        linearLayout.setVisibility(View.GONE);
        Toast.makeText(this.getContext(), error, Toast.LENGTH_LONG).show();
    }

    public void showSuccess(JSONObject result) {
        msgRv.setVisibility(View.GONE);
        linearLayout.setVisibility(View.VISIBLE);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("当前餐次：" + result.getJSONObject("bmcm").getString("name"));
        stringBuffer.append(" CardNo:" + result.getJSONObject("fk").getString("cardno"));
        stringBuffer.append("  姓名：" + result.getJSONObject("fk").getString("name"));
        stringBuffer.append("  卡余额：" + result.getJSONObject("fk").getString("kye"));
        stringBuffer.append("  补贴金额：" + result.getJSONObject("fk").getString("btye"));
        dateTopTv.setText(stringBuffer.toString());

        List<LcReportDto> list = result.getJSONArray("lcData").toJavaList(LcReportDto.class);
        if (list.size() == 0) {
            recyclerView.setVisibility(View.GONE);
            dateMsgLinear.setVisibility(View.VISIBLE);
            return;
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            dateMsgLinear.setVisibility(View.GONE);
        }
        recyclerView.setAdapter(lcReportAdapter);
        lcReportAdapter.setLcReportDtoList(list);
    }

}
